package com.mmall.service.Impl;

import java.io.File;
import java.util.Date;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import com.mmall.common.Const;
import com.mmall.common.ServerResponse;
import com.mmall.service.SendMailService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
/**
 * @Description:发邮件
 * @author: 赖美芳
 * @time:2018年3月12日 下午9:10:35
 */

@Service
public class SendMailServiceImpl implements SendMailService {

    private static final Logger logger = LoggerFactory.getLogger(SendMailServiceImpl.class);

    @Resource
    JavaMailSender mailSender;
    @Resource
    SimpleMailMessage simpleMailMessage;

    /**
     *
     * @Description:普通文本发邮件形式
     * @param subject 主题
     * @param content 正文
     * @param toMail 收件人邮箱
     * void
     * @exception:
     * @author: 赖美发
     * @time:2018年3月12日 下午9:10:35
     */
    @Override
    public ServerResponse sendSimpleMail(String subject,String content,String toMail) {
        simpleMailMessage.setSubject(subject);
        simpleMailMessage.setText(content);
        simpleMailMessage.setTo("15520673411@163.com");//默认把邮件发给自己。为了解决554错误
        simpleMailMessage.setCc(toMail);//设置抄送人为接收邮箱
        simpleMailMessage.setFrom(Const.FROM_MAIL_ADDRESS);
        try {
            mailSender.send(simpleMailMessage);
            logger.info("邮件发送成功..");
            return ServerResponse.cerateBySuccessMessage("验证码发送成功");
        }catch (Exception e){
            logger.info("邮件发送失败");
            return ServerResponse.createByErrorMessage("验证码发送失败");
        }


    }

    /**
     *
     * @Description:html发邮件形式
     * @param subject 主题
     * @param content 正文
     * @param toMail 收件人邮箱
     * void
     * @exception:
     * @author: fengjk
     * @time:2017年4月20日 下午8:06:38
     */
    public ServerResponse sendHtmlMail(String subject,String content,String toMail) {
        MimeMessage mailMessage = mailSender.createMimeMessage();
        MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage);
        try {
            messageHelper.setTo(toMail);
//            messageHelper.setCc("15520673411@163.com");
            messageHelper.setSubject(subject);
            messageHelper.setText("<html><head></head><body><h1>"+content+"</h1></body></html>",true);
            messageHelper.setFrom(Const.FROM_MAIL_ADDRESS);
            messageHelper.setSentDate(new Date());// 设置发信时间
            mailSender.send(mailMessage);
            logger.info("邮件发送成功..");
            return ServerResponse.cerateBySuccessMessage("邮件发送成功");
        } catch (MessagingException e) {
            e.printStackTrace();
            return ServerResponse.createByErrorMessage("验证码发送失败");
        }
    }

    /**
     *
     * @Description: 带图片发邮件形式
     * @param subject 主题
     * @param content 正文
     * @param toMail 收件人邮箱
     * @param picturePath 图片路径
     * void
     * @exception:
     * @author: 赖美芳
     * @time:2018年3月12日 下午9:11:40
     */
    public ServerResponse sendPictureMail(String subject,String content,String toMail,String picturePath){
        MimeMessage mailMessage = mailSender.createMimeMessage();
        MimeMessageHelper messageHelper;
        try {
            messageHelper = new MimeMessageHelper(mailMessage,true);
            messageHelper.setTo(toMail);
            messageHelper.setSubject(subject);
            messageHelper.setFrom(Const.FROM_MAIL_ADDRESS);
            //messageHelper.setText("<html><head></head><body><h1>" + content +"</h1><img src=/"+cid:aaa + /"/></body></html>",true);
            FileSystemResource img = new FileSystemResource(new File(picturePath));
            messageHelper.addInline("aaa",img);
            //发送邮件
            mailSender.send(mailMessage);
            logger.info("邮件发送成功..");
            return ServerResponse.cerateBySuccessMessage("邮件发送成功");
        } catch (MessagingException e) {
            e.printStackTrace();
            return ServerResponse.createByErrorMessage("验证码发送失败");
        }

    }

    /**
     *
     * @Description:带附件发邮件形式
     * @param subject 标题
     * @param content 正文
     * @param toMail 收件人邮箱
     * @param accessoryPath 附件路径
     * @param accessoryName 附件名
     * void
     * @exception:
     * @author: 赖美芳
     * @time:2018年3月12日 下午9:12:35
     */
    public ServerResponse sendMailTakeAccessory(String subject, String content, String toMail, String accessoryPath, String accessoryName){
        MimeMessage mailMessage = mailSender.createMimeMessage();
        MimeMessageHelper messageHelper;
        try {
            messageHelper = new MimeMessageHelper(mailMessage,true,"utf-8");
            messageHelper.setTo(toMail);
            messageHelper.setSubject(subject);
            messageHelper.setFrom(Const.FROM_MAIL_ADDRESS);
            messageHelper.setText("<html><head></head><body><h1>"+content+"</h1></body></html>",true);
            FileSystemResource file = new FileSystemResource(new File(accessoryPath));
            messageHelper.addAttachment(accessoryName,file);
            mailSender.send(mailMessage);
            logger.info("邮件发送成功..");
            return ServerResponse.cerateBySuccess("邮件发送成功");
        } catch (MessagingException e) {
            e.printStackTrace();

        }
        return ServerResponse.createByErrorMessage("邮件发送失败");
    }
}
